/****************************************************************************
 Copyright (c) 2010-2012 cocos2d-x.org
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011      Zynga Inc.

 http://www.cocos2d-x.org

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

var Anchor = {};
Anchor.LEFT_TOP = cc.p(0, 1);
Anchor.RIGHT_TOP = cc.p(1, 1);
Anchor.RIGHT_BOTTOM = cc.p(1, 0);
Anchor.LEFT_BOTTOM = cc.p(0, 0);
Anchor.MIDDLE = cc.p(0.5, 0.5);

var MyFifthApp = cc.LayerColor.extend({
    _sprite: null,
    _spriteFrameDir: 0, // 0 left, 1 right
    _spriteFrameIndex: 0,
    init: function () {
        var size = cc.Director.getInstance().getWinSize();
        this._super(cc.c4(0, 0, 0, 255), size.width, size.height);

        cc.SpriteFrameCache.getInstance().addSpriteFrames(s_spritesheet_plist, s_spritesheet);

        this._sprite = cc.Sprite.createWithSpriteFrameName(this.getSpriteFrameName());
        this._sprite.setBlendFunc(gl.SRC_ALPHA, gl.ONE);
        this._sprite.setPosition(cc.p(300, 300));
        this._sprite.setScale(3);
        this.addChild(this._sprite);

        var helloLabel2 = cc.LabelTTF.create("Menu", "Arial", 38);
        var menuItem = cc.MenuItemLabel.create(helloLabel2, this.onMenuItem);
        var menu2 = cc.Menu.create(menuItem);
        menu2.setPosition(cc.p(50, 150));
        this.addChild(menu2);

        this.setKeyboardEnabled(true);
        return this;
    },
    getSpriteFrameName: function () {
        var indexAsString;
        if (this._spriteFrameIndex < 10)
            indexAsString = "0" + this._spriteFrameIndex.toString();
        else
            indexAsString = this._spriteFrameIndex.toString();
        var frameNamePrefix;
        if (this._spriteFrameDir == 0)
            frameNamePrefix = "Walk_left";
        else
            frameNamePrefix = "Walk_right";
        return frameNamePrefix + indexAsString + ".png";
    },
    onMenuItem: function (pSender) {
        var scene2 = new MyLayer();
        scene2.init();
        var tranScene = cc.TransitionMoveInL.create(0.5, scene2);
        cc.Director.getInstance().replaceScene(tranScene);
    },
    onKeyUp: function (e) { },
    onKeyDown: function (e) {
        if (e == cc.KEY.left || e == cc.KEY.right) {
            var prevPrefix = this._spriteFrameDir;
            this._spriteFrameDir = (e == cc.KEY.left) ? 0 : 1;

            if (prevPrefix !== this._spriteFrameDir)
                this._spriteFrameIndex = 0;

            if (this._spriteFrameIndex > 18)
                this._spriteFrameIndex = 0;

            this.removeChild(this._sprite);
            this._sprite = cc.Sprite.createWithSpriteFrameName(this.getSpriteFrameName());
            this._sprite.setPosition(cc.p(300, 300));
            this._sprite.setScale(3);
            this.addChild(this._sprite);
            this._spriteFrameIndex++;
        }
    }
});

var YellowLayer = cc.Layer.extend({
    _jetSprite:null,
    init: function () {
        this._super();

        var size = cc.Director.getInstance().getWinSize();
        var background = cc.LayerColor.create(cc.c4(0, 0, 0, 255), size.width, size.height);
        this.addChild(background);

        var helloLabel2 = cc.LabelTTF.create("Menu", "Arial", 38);
        var menuItem = cc.MenuItemLabel.create(helloLabel2, this.onMenuItem);
        var menu2 = cc.Menu.create(menuItem);
        menu2.setPosition(cc.p(50, 150));
        this.addChild(menu2);

        this._jetSprite = new JetSprite();
        this.setKeyboardEnabled(true);

        this.setPosition(new cc.Point(0,0));

        this.addChild(this._jetSprite);
        this._jetSprite.setPosition(new cc.Point(size.width/2,size.height/2));
        this._jetSprite.scheduleUpdate();
//        this.schedule(this.update);

        return true;
    },

//     update:function(dt){
//     },

    onKeyDown:function(e){
        this._jetSprite.handleKey(e);
    },

    onMenuItem: function (pSender) {
        var scene2 = new MyFifthApp();
        scene2.init();
        var tranScene = cc.TransitionMoveInR.create(0.5, scene2);
        cc.Director.getInstance().replaceScene(tranScene);
    }
});

var MyLayer = cc.Layer.extend({
    init: function () {
        this._super();

        var size = cc.Director.getInstance().getWinSize();
        cc.log(size);

        var background = cc.LayerColor.create(cc.c4(0, 0, 0, 255), size.width, size.height);
        this.addChild(background);

        // add a sprite image
        this.sprite = cc.Sprite.create("res/psb.jpg");
        this.sprite.setPosition(cc.p(0, 0));
        this.sprite.setAnchorPoint(Anchor.LEFT_BOTTOM);
        this.addChild(this.sprite, 0);

        // add a "close" icon to exit the progress. it's an autorelease object
        var closeItem = cc.MenuItemImage.create(
            "res/CloseNormal.png",
            "res/CloseSelected.png",
            function () {
                history.go(-1);
            }, this);
        closeItem.setAnchorPoint(Anchor.LEFT_BOTTOM);

        var menu = cc.Menu.create(closeItem);
        menu.setPosition(cc.PointZero());
        this.addChild(menu, 1);

        // add a label shows "Hello World"
        // create and initialize a label
        var helloLabel = cc.LabelTTF.create("This is Label", "Arial", 38);
        // position the label on the center of the screen
        helloLabel.setAnchorPoint(Anchor.LEFT_BOTTOM);
        helloLabel.setPosition(cc.p(10, 100));
        // add the label as a child to this layer
        this.addChild(helloLabel, 2);

        var helloLabel2 = cc.LabelTTF.create("Menu", "Arial", 38);
        var menuItem = cc.MenuItemLabel.create(helloLabel2, this.onMenuItem);
        var menu2 = cc.Menu.create(menuItem);
        menu2.setPosition(cc.p(50, 150));
        this.addChild(menu2, 3);

        cc.MenuItemFont.setFontName("Arial");
        cc.MenuItemFont.setFontSize(18);
        var title2 = cc.MenuItemFont.create("Mode");
        title2.setEnabled(false);

        cc.MenuItemFont.setFontName("Arial");
        cc.MenuItemFont.setFontSize(26);
        this.aryItem = new Array("ScaleBy", "RotateBy", "Blink", "TintBy", "FadeOut");
        var item2 = cc.MenuItemToggle.create(
                cc.MenuItemFont.create(this.aryItem[0]),
                cc.MenuItemFont.create(this.aryItem[1]),
                cc.MenuItemFont.create(this.aryItem[2]),
                cc.MenuItemFont.create(this.aryItem[3]),
                cc.MenuItemFont.create(this.aryItem[4]));
        item2.setCallback(this.onModeControl, this);
        item2.setSelectedIndex(3);
        var menu3 = cc.Menu.create(title2, item2);
        menu3.alignItemsInRows(2);
        menu3.setPosition(cc.p(50, 420));
        this.addChild(menu3);

        this.setKeyboardEnabled(true);
        return true;
    },

    onMenuItem: function (pSender) {
        var scene2 = new YellowLayer();
        scene2.init();
        var tranScene = cc.TransitionMoveInL.create(0.5, scene2);
        cc.Director.getInstance().replaceScene(tranScene);
    },

    onModeControl: function (pSender) {
        var selectIndex = pSender.getSelectedIndex();
        cc.log("click menu index: " + selectIndex + " " + this.aryItem[selectIndex]);
        this.spriteAction(selectIndex);
    },

    onKeyDown: function (Key) {
        cc.log("onKeyDown : " + Key);
    },

    spriteAction: function (nIndex) {
        var action;
        switch (nIndex) {
            case 0:
                action = cc.ScaleBy.create(3, 2);
                break;
            case 1:
                action = cc.RotateBy.create(3, 360);
                break
            case 2:
                action = cc.Blink.create(1, 3);
                break;
            case 3:
                action = cc.TintBy.create(2, 0, -255, -255);
                break;
            default:
                action = cc.FadeOut.create(2);
                break;
        }
        var action_back = action.reverse();
        var seq = cc.Sequence.create(action, action_back, null);
        this.sprite.runAction(cc.Repeat.create(seq, 1));
    }
});

var MyScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new MyLayer();
        layer.init();
        this.addChild(layer);
    }
});

